package org.convallaria.system.biz.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 混合缓存测试服务
 * 用于测试hybrid缓存的功能
 * 
 * @author gjh
 * @since 2024-09-25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HybridCacheTestService {

    /**
     * 测试缓存获取
     */
    @Cacheable(value = "hybrid-test", key = "#id")
    public String getCachedData(String id) {
        log.info("执行数据库查询，ID: {}", id);
        // 模拟数据库查询延迟
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "数据-" + id + "-" + LocalDateTime.now();
    }

    /**
     * 测试缓存更新
     */
    @CacheEvict(value = "hybrid-test", key = "#id")
    public void evictCache(String id) {
        log.info("清除缓存，ID: {}", id);
    }

    /**
     * 测试缓存清空
     */
    @CacheEvict(value = "hybrid-test", allEntries = true)
    public void clearAllCache() {
        log.info("清空所有缓存");
    }

    /**
     * 测试随机数据
     */
    @Cacheable(value = "hybrid-random", key = "#seed")
    public String getRandomData(int seed) {
        log.info("生成随机数据，种子: {}", seed);
        return "随机数据-" + ThreadLocalRandom.current().nextInt(1000) + "-" + LocalDateTime.now();
    }

    /**
     * 测试复杂对象缓存
     */
    @Cacheable(value = "hybrid-complex", key = "#type + ':' + #id")
    public ComplexData getComplexData(String type, String id) {
        log.info("查询复杂数据，类型: {}, ID: {}", type, id);
        // 模拟数据库查询延迟
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return ComplexData.builder()
                .id(id)
                .type(type)
                .value("复杂数据-" + ThreadLocalRandom.current().nextInt(10000))
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 测试异步缓存操作
     */
    @Cacheable(value = "hybrid-async", key = "#id")
    public CompletableFuture<String> getAsyncData(String id) {
        log.info("异步查询数据，ID: {}", id);
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return "异步数据-" + id + "-" + LocalDateTime.now();
        });
    }

    /**
     * 测试条件缓存
     */
    @Cacheable(value = "hybrid-conditional", key = "#id", condition = "#id.length() > 3")
    public String getConditionalData(String id) {
        log.info("条件查询数据，ID: {}", id);
        try {
            Thread.sleep(80);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "条件数据-" + id + "-" + LocalDateTime.now();
    }

    /**
     * 测试缓存统计
     */
    public void printCacheStats() {
        log.info("缓存统计信息打印完成");
    }

    /**
     * 复杂数据对象
     */
    @lombok.Data
    @lombok.Builder
    @lombok.NoArgsConstructor
    @lombok.AllArgsConstructor
    public static class ComplexData {
        private String id;
        private String type;
        private String value;
        private LocalDateTime timestamp;
        
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            ComplexData that = (ComplexData) o;
            return Objects.equals(id, that.id) && 
                   Objects.equals(type, that.type) && 
                   Objects.equals(value, that.value);
        }
        
        @Override
        public int hashCode() {
            return Objects.hash(id, type, value);
        }
    }
}
